<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <!-- 表单 -->
        <el-form ref="userInfoRef" label-width="120px" :model="userInfoForm" :rules="userInfoRules" style="margin-left: 120px; margin-top:30px">
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="userInfoForm.username" style="width:300px" />
          </el-form-item>

          <el-form-item label="手机">
            <el-input v-model="userInfoForm.mobile" disabled style="width:300px" />
          </el-form-item>

          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfoForm.timeOfEntry"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>

          <el-form-item label="员工头像">
            <!-- 上传头像组件 -->
            <!-- 自定义事件行内写法：$event 就是子组件传递过来的数据 -->
            <!-- <upload-img :url="userInfoForm.staffPhoto" @get-url="userInfoForm.staffPhoto = $event"></upload-img> -->
            <!-- <upload-img :value="userInfoForm.staffPhoto" @input="userInfoForm.staffPhoto = $event"></upload-img> -->
            <!-- 组件上 v-model 的使用场景 -->
            <!-- 1、一个自定义属性的绑定，和一个自定义事件的监听 -->
            <!-- 2、自定义事件中修改的数据，是自定义属性绑定的数据 -->
            <upload-img v-model="userInfoForm.staffPhoto"></upload-img>
          </el-form-item>

          <!-- 保存个人信息 -->
          <el-row class="inline-info" type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" @click="submit">保存更新</el-button>
              <el-button @click="$router.back()">返回</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserInfoDetail } from '@/api/user'
import UploadImg from '@/components/UploadImg'
import { updateEmployee } from '@/api/employee'

export default {
  components: {
    UploadImg
  },
  data() {
    return {
      // 表单数据对象
      userInfoForm: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      // 表单验证规则对象
      userInfoRules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // console.log(this.$route.query.id)
    // 获取员工的详细信息
    const res = await getUserInfoDetail(this.$route.query.id)
    // console.log(res)
    // 数据回显，给表单数据对象赋值
    this.userInfoForm = res.data
  },
  methods: {
    // getUrl(imgUrl) {
    //   this.userInfoForm.staffPhoto = imgUrl
    // }
    submit() {
      // 兜底校验
      this.$refs.userInfoRef.validate(async(valid) => {
        if (!valid) return
        // 校验通过
        // 发起请求
        await updateEmployee(this.userInfoForm)
        // 提示用户
        this.$message.success('更新员工信息成功')
      })
    }
  }
}
</script>

<style scoped>

</style>
